package stu.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import stu.aistar.dao.IMovieDao;
import stu.aistar.dao.IMovieTypeDao;
import stu.aistar.model.pojo.Movie;
import stu.aistar.model.pojo.Movietype;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/movie")
public class MovieGridController {

    @Autowired
    private IMovieTypeDao iMovieTypeDao;

    @Autowired
    private IMovieDao iMovieDao;

    @GetMapping("/grid")
    public String grid(Model model,String k){

        List<Movie> movieLists=new ArrayList<>();
        System.out.println(k);
        List<Movietype> list = iMovieTypeDao.findAll();
        model.addAttribute("list",list);

//        Movietype movietype = iMovieTypeDao.findMovietypeById(1);
//        model.addAttribute("movietype",movietype);

        if(k==""||k==null){
            System.out.println("k是空");
            List<Movie> list1 = iMovieDao.findAll();
            model.addAttribute("list1",list1);
        }else{
            System.out.println("k不是空");
            char[] kk=k.toCharArray();
            Integer[] checks=new Integer[kk.length];
            for(int i=0;i<kk.length;i++) {
                Integer typeId = Integer.valueOf(kk[i]-'0');
                checks[i]=typeId;
                System.out.println(typeId);
                List<Movie> movieList = iMovieDao.findMovieByMovietypeId(typeId);
                for (Movie movie : movieList) {
                    movieLists.add(movie);
                }
            }
            model.addAttribute("list1",movieLists);
            model.addAttribute("checks",checks);
        }
        return "movie-grid";
    }


}
